﻿using SPWebShell.entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Web.Services.Protocols;

namespace SPWebShell.cmdlet
{
    [Cmdlet("Checkin", "spfile")]
    public class SPCheckinFile : CmdLetBase
    {
        public SPCheckinFile()
        {

        }
        [Parameter(Mandatory = true, HelpMessage = "The absolute URL to the document. e.g. http://server/sites/Documents/abc.docx")]
        [ValidateNotNullOrEmpty]
        [Alias("url")]
        public string DocumentUrl { get; set; }

        [Parameter(Mandatory = true, HelpMessage = "The absolute URL to the document. e.g. http://server/sites/Documents/abc.docx")]
        [Alias("comments")]
        public string CheckinComments { get; set; }

        [Parameter(Mandatory = true, HelpMessage = "0=Minor Checkin, 1=Major Checkin , 2=Overwrite Checkin")]
        public int CheckinType { get; set; }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            try
            {
                VerboseLogger("Inside Cmdlet checkin-spfile");
                VerboseLogger("Start time: " + DateTime.Now.ToString());
                ISPRepository repos = base.SPRepos;
                repos.CheckinFile(this.DocumentUrl, this.CheckinComments, this.CheckinType);
                VerboseLogger("End time: " + DateTime.Now.ToString());
                base.ProcessRecord();
            }
            catch (SoapException ex)
            {
                string message = ex.Detail.OuterXml;
                throw new Exception(message, ex);
            }
            finally
            {
                VerboseLogger("Exiting Cmdlet CheckinFile");
            }
        }
    }
}
